package cn.xiyue.production.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import cn.xiyue.production.R;
import cn.xiyue.production.base.CallableActivity;
import cn.xiyue.production.bean.Person;
import cn.xiyue.production.bean.Position;

public class AutoFillActivity extends CallableActivity {

    private Map<String, TextView> textViewMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_auto_fill);
        initView();
        getData();
        setClick();
    }

    private void getData() {
        async(requestApi.getAllPosition(2, 0), value -> {
            List<Position> data = value.getData();
            for (Position position : data) {
                String key = Integer.toString(position.getPositionId()).substring(1);
                TextView view = textViewMap.get(key);
                if (position.getUserPersonId() != 0) {
                    async(requestApi.getPersonInfo(position.getUserPersonId()), value1 -> {
                        Person person = value1.getData().get(0);
                        setUser(Objects.requireNonNull(view), person);
                    });
                }
                Objects.requireNonNull(view).setTag(position);
            }
        });
    }

    private void initView() {
        textViewMap = new HashMap<>();
        textViewMap.put("01", (TextView) findViewById(R.id.tv_01));
        textViewMap.put("02", (TextView) findViewById(R.id.tv_02));
        textViewMap.put("03", (TextView) findViewById(R.id.tv_03));
        textViewMap.put("04", (TextView) findViewById(R.id.tv_04));
        textViewMap.put("11", (TextView) findViewById(R.id.tv_11));
        textViewMap.put("12", (TextView) findViewById(R.id.tv_12));
        textViewMap.put("13", (TextView) findViewById(R.id.tv_13));
        textViewMap.put("14", (TextView) findViewById(R.id.tv_14));
        textViewMap.put("21", (TextView) findViewById(R.id.tv_21));
        textViewMap.put("22", (TextView) findViewById(R.id.tv_22));
        textViewMap.put("23", (TextView) findViewById(R.id.tv_23));
        textViewMap.put("24", (TextView) findViewById(R.id.tv_24));
        textViewMap.put("31", (TextView) findViewById(R.id.tv_31));
        textViewMap.put("32", (TextView) findViewById(R.id.tv_32));
        textViewMap.put("33", (TextView) findViewById(R.id.tv_33));
        textViewMap.put("34", (TextView) findViewById(R.id.tv_34));
    }

    private void setClick() {
        for (Map.Entry<String, TextView> textViewEntry : textViewMap.entrySet()) {
            TextView value = textViewEntry.getValue();
            value.setOnClickListener(v -> {
                Position v1 = (Position) v.getTag();
                if (v1 != null) {
                    async(requestApi.addUserPosition(v1.getPositionId()), value1 -> {
                        setUser(v, value1.getData().get(0));
                    });
                }
            });
        }
    }

    private void setUser(View v, Person person) {
        TextView v1 = (TextView) v;
        v.setBackgroundColor(getResources().getColor(R.color.fill_background));
        v1.setText(String.format("姓名：%s\n体力：%d", person.getPersonName(), person.getPower()));
        v.setOnClickListener(v2 -> showToast("岗位上已存在人员"));
    }
}